package hut.lvming.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import hut.lvming.dao.BookMapper;
import hut.lvming.dao.RecordMapper;
import hut.lvming.domain.Book;
import hut.lvming.domain.Record;
import hut.lvming.domain.User;
import hut.lvming.entity.PageResult;
import hut.lvming.entity.Result;
import hut.lvming.service.BookService;
import hut.lvming.service.RecoedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class BookServiceImpl implements BookService {
    @Autowired(required = false)
    private BookMapper bookMapper;
    @Autowired(required = false)
    private RecoedService recordService;

    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page=bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Book findById(String id) {
        return bookMapper.findById(id);
    }

    @Override
    public Integer borrowBopok(Book book) {
        Book b=this.findById(book.getId()+"");

        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(dateFormat.format(new Date()));
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadTime(b.getUploadTime());
        System.out.println(book);
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page=bookMapper.searchBooks(book);
//        System.out.println("**********************BookServiceImpl.search()************************************");
//        System.out.println(page.getTotal());
//        System.out.println(page.getResult());
//        System.out.println("**********************BookServiceImpl.search()************************************");
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer addBook(Book book) {
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        book.setUploadTime(dateFormat.format(new Date()));
        System.out.println("**********************BookServiceImpl.addBook()************************************");
        System.out.println(book);
        System.out.println("**********************BookServiceImpl.addBook()************************************");
        return bookMapper.addBook(book);
    }

    @Override
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }

    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Book> page;
        book.setBorrower(user.getName());
        if("ADMIN".equals(user.getRole())){
            page=bookMapper.selectBorrowed(book);
        }else{
            page=bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public boolean returnBook(String id, User user) {
        Book book=bookMapper.findById(id);
        boolean rb=book.getBorrower().equals(user.getName());
        if(rb){
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }

    @Override
    public Integer returnConfirm(String id) {
        Book book=bookMapper.findById(id);
        Record record=this.setRecoord(book);
        book.setStatus("0");
        book.setBorrower("");
        book.setBorrowTime("");
        book.setReturnTime("");
        Integer count=bookMapper.editBook(book);
        if(count==1){
            return recordService.addRecord(record);
        }
        return 0;
    }

    private Record setRecoord(Book book){
        Record record=new Record();
        record.setBookname(book.getName());
        record.setBookisbn(book.getIsbn());
        record.setBorrower(book.getBorrower());
        record.setBorrowTime(book.getBorrowTime());
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        record.setRemandTime(dateFormat.format(new Date()));
        return record;
    }
}
